package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.Msg;
import com.bean.Orders;
import com.bean.User;
import com.dao.OrderMapper;
import com.service.impl.OrderServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用于订单表的后台管理
 * @author shkstart
 * @create 2022-06-20-23:45
 */
@RestController
@RequestMapping("/manage")
public class OrderManageController {
    @Resource
    OrderMapper orderMapper;

    @Resource
    OrderServiceImpl orderService;

    //分页+模糊查找订单

    @GetMapping("/orderPage")
    public IPage<Orders> goodsPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(required = false) Integer ouId,//前端不传该参数时 默认为""
                                 @RequestParam(required = false) Integer orderPrice){
        IPage<Orders> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        //当数据库中该数据为null也要模糊匹配查询
        //当传入了用户id时候 只查找该用户的订单信息
        System.out.println(ouId);
        queryWrapper.eq(ouId!=null&&ouId>0,"ou_id",ouId);
        //当传入了订单金额时候 查询>=订单金额的订单
        queryWrapper.ge(orderPrice!=null&&orderPrice>0,"order_price",orderPrice);
        IPage<Orders> ordersPage = orderService.page(page, queryWrapper);
        return ordersPage;
    }

    /**
     * 后台冻结订单的状态
     */
    @GetMapping("/freezeOrder")

    public boolean doFreeze(@RequestParam Integer orderId){
        //获取订单信息
        Orders order = orderService.getById(orderId);
        //将订单状态设置为2 即为异常
        order.setOrderStatus(2);
        //在数据库中修改
        return orderService.updateById(order);
    }

    /**
     * 后台完成的状态
     */
    @GetMapping("/activateOrder")
    public boolean doActivate(@RequestParam Integer orderId){
        Orders order = orderService.getById(orderId);
        //将订单状态设置为1 即为进行
        order.setOrderStatus(1);
        return orderService.updateById(order);
    }

    /**
     * 后台进行订单的状态
     */
    @GetMapping("/doneOrder")
    public boolean done(@RequestParam Integer orderId){
        Orders order = orderService.getById(orderId);
        //将订单状态设置为0 即为完成
        order.setOrderStatus(0);
        return orderService.updateById(order);
    }

    /**
     * 删除该订单以及orders_goods表中含有该订单的记录
     */
    @DeleteMapping("/deleteOrder/{id}")
    public Msg deleteOrder(@PathVariable Integer id){
        if(orderService.getById(id)==null) return Msg.fail().add("cause","没有该订单");
            orderService.removeById(id);
            //记录删除的记录数
            Integer counts = orderMapper.deleteOrders_goodsByOrder(id);
            return Msg.success().add("counts",counts);
    }
}
